using System;
using System.ComponentModel;
using System.Globalization;
using Microsoft.WowAddonStudio.FrameXml.Serialization.TypeConverters;
using NUnit.Framework;

namespace Microsoft.WowAddonStudio.FrameXml.Serialization.Tests.TypeConvertersTests
{
	[TestFixture]
	public sealed class ColorTypeConverterTest
	{
		private static readonly char listSeparator = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0];
		private TypeConverter converter;

		#region SetUp

		[SetUp]
		public void SetUp()
		{
			converter = new ColorTypeConverter();
		}

		#endregion

		#region TearDown

		[TearDown]
		public void TearDown()
		{
		}

		#endregion

		#region Tests

		[Test(Description = "")]
		public void Test_ColorTypeConverterSimple()
		{
			converter = new ColorTypeConverter();
			var color = new Color { Alpha = 1, Red = 1, Blue = 1, Green = 0.5f };

			object convertTo = converter.ConvertTo(null, CultureInfo.InvariantCulture, color, typeof(string));
			Assert.IsNotNull(convertTo);
			Assert.IsInstanceOfType(typeof(string), convertTo);
			Console.WriteLine(convertTo);
			Assert.AreEqual("255" + listSeparator + "128" + listSeparator + "255", convertTo);

			object convertFrom = converter.ConvertFrom(null, CultureInfo.InvariantCulture, "255" + listSeparator + "128" + listSeparator + "255");
			Assert.IsNotNull(convertFrom);
			Assert.IsInstanceOfType(typeof(Color), convertFrom);
			Console.WriteLine(convertFrom);
			var convertFromColor = (Color)convertFrom;

			Assert.AreEqual(color.Alpha, convertFromColor.Alpha);
			Assert.AreEqual(color.Red, convertFromColor.Red);
			Assert.AreEqual(color.Green, convertFromColor.Green);
			Assert.AreEqual(color.Blue, convertFromColor.Blue);

			color = new Color { Alpha = 1, Red = 0, Blue = 0, Green = 0.5f };
			convertTo = converter.ConvertTo(null, CultureInfo.InvariantCulture, color, typeof(string));
			Assert.IsNotNull(convertTo);
			Assert.IsInstanceOfType(typeof(string), convertTo);
			Console.WriteLine(convertTo);
			Assert.AreEqual("Green", convertTo);
		}

		[Test(Description = "")]
		public void Test_ColorTypeConverterConvertFromKnownColor()
		{
			converter = new ColorTypeConverter();
			object convertFrom = converter.ConvertFrom(null, CultureInfo.InvariantCulture, "Yellow");
			Assert.IsNotNull(convertFrom);
			Assert.IsInstanceOfType(typeof(Color), convertFrom);
			var color = (Color)convertFrom;
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);
			Assert.AreEqual(1, color.Alpha);
			Assert.AreEqual(1, color.Red);
			Assert.AreEqual(0, color.Blue);
			Assert.AreEqual(1, color.Green);

			convertFrom = converter.ConvertFrom(null, CultureInfo.InvariantCulture, "OliveDrab");
			Assert.IsNotNull(convertFrom);
			Assert.IsInstanceOfType(typeof(Color), convertFrom);
			color = (Color)convertFrom;
			Console.WriteLine("A:{0},R:{1},G:{2},B:{3}", color.Alpha, color.Red, color.Green, color.Blue);

		}
		[Test(Description = "")]
		[ExpectedException(typeof(ArgumentException))]
		public void Test_ColorTypeConverterConvertFromKnownColorFailed()
		{
			converter = new ColorTypeConverter();
			converter.ConvertFrom(null, CultureInfo.InvariantCulture, "e5yr5y");
		}

		#endregion
	}
}